import mongoose, { Schema } from 'mongoose'
import { INote } from '../types'

const NoteSchema = new Schema<INote>({
  userId: {
    type: String,
    required: [true, '用户ID是必填项'],
    ref: 'User'
  },
  title: {
    type: String,
    required: [true, '标题是必填项'],
    trim: true,
    maxlength: [200, '标题最多200个字符']
  },
  content: {
    type: String,
    required: [true, '内容是必填项']
  },
  tags: [{
    type: String,
    trim: true,
    maxlength: [50, '标签最多50个字符']
  }],
  isPublic: {
    type: Boolean,
    default: false
  }
}, {
  timestamps: true
})

// 索引优化
NoteSchema.index({ userId: 1, createdAt: -1 })
NoteSchema.index({ title: 'text', content: 'text' })
NoteSchema.index({ tags: 1 })

export const Note = mongoose.model<INote>('Note', NoteSchema)